﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.DAO;
using ClassLibrary.Entity;

public partial class Admin_ucControl_ucDSLK : System.Web.UI.UserControl
{
    TblDSLKDAO DSLKDAO = new TblDSLKDAO(Global.CONNECTION_STRING);

    protected void Page_Load(object sender, EventArgs e)
    {
        string id = Request.QueryString["id"];
        if (!IsPostBack)
        {
            if (Session[Global.SESSION_ADMIN_USER] == "" || Session[Global.SESSION_ADMIN_USER] == null)
            {
                Response.Redirect("~/Admin/Login.aspx");
            }
            if (id != null)
            {
                LoadDSLK();
                LoadDetailDSLK();

            }
            else
            {
                LoadDSLK();
            }
        }
    }

    private void LoadDetailDSLK()
    {
        string id = Request.QueryString["id"];
        TblDSLK dslk = DSLKDAO.GetDSLKByID(Global.ConvertToInt(id));
        txtTen.Text = dslk.Name;
        txtdiachiweb.Text = dslk.NavigateUrl;
    }
    protected void btAdd_Click(object sender, EventArgs e)
    {
        bool flag = false;
        try
        {
            string id = Request.QueryString["id"];
            TblDSLK dslk = new TblDSLK();
            dslk.Name = txtTen.Text;
            dslk.NavigateUrl = txtdiachiweb.Text;

            if (id == null || id == "")
            {
                dslk.ID = 0;
                DSLKDAO.InsUpDSLK(dslk, 1);
            }
            else
            {
                dslk.ID = Convert.ToInt32(id);
                DSLKDAO.InsUpDSLK(dslk, 2);
            }
            flag = true;
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE_ADMIN);
        }
        if (flag)
        {
            LoadDSLK();
        }
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        txtdiachiweb.Text = string.Empty;
        txtTen.Text = string.Empty;
    }
    protected void grdDSLK_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdDSLK.PageIndex = e.NewPageIndex;
        LoadDSLK();
    }

    private void LoadDSLK()
    {
        try
        {
            grdDSLK.DataSource = DSLKDAO.GetDSLK();
            grdDSLK.DataBind();
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE_ADMIN);
        }
    }

    protected void btnDelImg_Click(object sender, ImageClickEventArgs e)
    {
        string id = Request.QueryString["id"];
        bool flag = true;
        try
        {
            TblDSLK dslk = new TblDSLK();

            dslk.ID = Convert.ToInt32(((ImageButton)sender).CommandArgument);
            int idXoa = Convert.ToInt32(((ImageButton)sender).CommandArgument);

            dslk = DSLKDAO.GetDSLKByID(idXoa);
            DSLKDAO.DeleteDSLK(dslk);

            flag = true;

        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE_ADMIN);
        }

        if (flag)
        {
            LoadDSLK();
        }
    }
    protected void grdDSLK_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            foreach (DataControlFieldCell cell in e.Row.Cells)
            {
                // check all cells in one row
                foreach (Control control in cell.Controls)
                {
                    ImageButton button = control as ImageButton;
                    if (button != null && button.CommandName == "Delete")
                    {
                        // Add delete confirmation
                        button.CommandArgument = ((TblItemImage)e.Row.DataItem).ID.ToString();
                        button.OnClientClick = "if (!confirm('Bạn có thật sự muốn xóa không ?')) return false;";
                    }
                }
            }
        }
    }
}
